<?php
/**
 * Created by PhpStorm.
 * User: 子不语
 * Date: 2016/5/27
 * Time: 12:35
 */
include_once("inc/conn.php");
include_once("inc/function.php");
include_once("heheadmin/inc/config.php");
include_once 'payment/hj.php';
$Pay=new hj();
file_put_contents('test.txt',var_export($_REQUEST,true),FILE_APPEND );
file_put_contents('post.txt',var_export($_POST,true) ,FILE_APPEND);
//http://URL?tranxSN="+tranxSN+"&respCode="+respCode+"&respInfo="+respInfo+"&orderId="+orderId

if($_REQUEST['out_trade_no'] && ($_REQUEST['trade_status']=='TRADE_FINISHED' || $_REQUEST['trade_status']=='TRADE_SUCCESS')){
    $result=respond($_REQUEST['out_trade_no'], $_REQUEST['trade_no']);
    die($result);
}else if($_REQUEST['BillNo']){
    if($res=$Pay->respond()!==true) {
        die($res);
    }
    $payid=trim($_REQUEST['BillNo']);
    $orderNo=trim($_REQUEST['Result']);
    $sql = "call web_payonline_topay('{$payid}','{$orderNo}')";
    //WriteLog($sql);
    $arr = $db->Mysqli_Multi_Query($sql);
    $ret = $arr[0][0]["result"];
    switch($ret)
    {
        case '-1':
            $msg = "系统错误，请联系客服!";
            break;
        case '0':
            $msg = "ok";
            break;
        case '1':
            $msg = "订单不存在!请联系客服!";
            break;
        case '2':
            $msg = "ok";
            break;
        default:
            $msg = "未知错误，请联系客服!";
            break;
    }
    die($msg);

}

function respond($payid,$orderNo){
    global $db;
    $sql = "call web_payonline_topay('{$payid}','{$orderNo}')";
    //WriteLog($sql);
    $arr = $db->Mysqli_Multi_Query($sql);
    $ret = $arr[0][0]["result"];
    switch($ret)
    {
        case '-1':
            $msg = "系统错误，请联系客服!";
            break;
        case '0':
            $msg = "ok";
            break;
        case '1':
            $msg = "订单不存在!请联系客服!";
            break;
        case '2':
            $msg = "ok";
            break;
        default:
            $msg = "未知错误，请联系客服!";
            break;
    }
    return $msg;
}